/**
 * Created by Administrator on 2018/11/15 0015.
 */
import axios from 'axios'
import { Message ,Loading  } from 'element-ui'
import router from './router'

/*//加载动画
let loading;
function startLoading() {
  loading = Loading.service({
    lock:true,
    text:'拼命加载中',
    background:'rgba(0,0,0,0.7)',
    fullscreen:false
  });
}
function endLoading() {
  setTimeout(()=>{
    loading.close()
  });
}*/
//请求拦截
axios.interceptors.request.use(
  config =>{
    let mytoken = localStorage.mytoken;
    if(mytoken){ //判断是否存在token,如果存在的话，每个http header上都加token
      config.headers.Authorization = mytoken
    }
    console.log(config);
    return config
  },
  error => {
    //错误提醒
    return Promise.reject(error);

  }
);

//响应拦截
axios.interceptors.response.use(
  response =>{
    console.log(response);
    return response;
  },
  error => {
    //获取状态码
    console.log(error)
    const state = error.response.status;
    console.log(state)
    if(state ==401){
      Message.error("token失效请重新登录");
      //清除token
      localStorage.removeItem('mytoken');
      router.push('/')
    }
    return Promise.reject(error)
  }
);
export default axios;
